﻿/// <reference path="../_References.js" />
(function () {
    Lc.Models.AppViewModel = function () {
        var self = this;

        self.numberRowsViewModel = new Lc.Models.NumberRowsViewModel(self);
        self.numberFieldViewModel = new Lc.Models.NumberFieldViewModel(self);
        self.settingsViewModel = new Lc.Models.SettingsViewModel(self);
    };

    Lc.Models.NumberRowsViewModel = function (appViewModel) {
        var self = this,

            _loadRows = function () {
                console.log("loadRows");
                // Load rows
                Lc.NumbersDb.all(function (rows) {
                    var list = $(rows).map(function () { return this.value; });
                    self.numberRows(list);
                });
            };

        self.selected = ko.observable(new Lc.Models.NumberRowModel([]));

        self.numberRows = ko.observableArray([]);

        self.setCurrentRow = function (data) {
            console.log("setCurrentRow " + data.Id);
            self.selected(data);
        };

        self.canAddRows = ko.computed(function () {
            if (Lc.Settings.IsFreeVersion) {
                return self.numberRows().length < 2;
            }

            return true;
        });

        self.addRow = function () {
            console.log("addRow");

            var newNumbers = $("#newRowButtons input:checked").map(Lc.Utils.GetNumber).get(),
			    newRow = new Lc.Models.NumberRowModel(newNumbers);

            Lc.NumbersDb.get(newRow.Id, function (obj) {
                if (!obj) {
                    Lc.NumbersDb.save({ key: newRow.Id, value: newRow }, function (obj) {
                        console.log("Saved: " + newRow.Id);
                        self.numberRows.push(newRow);
                    });
                } else {
                    //TODO: Show message that row already exists.
                    console.log("Row already exists in database: " + newRow.Id);
                }
            });

            // Go back to the page that called.
            history.back(1);
        };

        self.deleteRow = function () {
            console.log("deleteRow");

            var row = self.selected();

            Lc.NumbersDb.remove(row.Id, function () {
                console.log("removed " + row.Id);

                self.numberRows.remove(row);

                $("#deleteRowDialog").dialog("close");
            });
        };

        _loadRows();
    };

    //TODO
    Lc.Models.NumberFieldViewModel = function (appViewModel) {
    };

    Lc.Models.SettingsViewModel = function (appViewModel) {
        var self = this;

        self.beepOnNotify = ko.computed({
            read: function () {
                return Lc.Settings.BeepOnNotify;
            },
            write: function (value) {
                Lc.SettingsDb.save({ key: "BeepOnNotify", value: value }, function (obj) {
                    Lc.Settings.BeepOnNotify = value;
                    console.log("Saved BeepOnNotify: " + value);
                });
            },
            owner: self
        });
    };
})();